<?php
//2011-02-09 update
//获取网站URL
function getSiteUrl()
{
	$root = strtolower(ROOT_PATH);
	$path = str_replace('\\', '/',strtolower(htmlspecialchars($_SERVER['SCRIPT_FILENAME'])));
	$url = strtolower(htmlspecialchars($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']));
	if(strtolower(PHP_OS) != 'winnt')
	{
		$roots = explode('/',$root);
		$paths = explode('/',$path);
		if($roots[1] != $paths[1])
		{
			$roots[1] = $paths[1];
			$root = implode('/',$roots);
		}

		$path = str_replace($root,'',$path);
		$url = substr(str_replace($path,'',$url),0,-1);
		return 'http://'.preg_replace("/\:\d+/", '', $_SERVER['HTTP_HOST']).($_SERVER['SERVER_PORT'] && $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '').$url;
	}
	else
	{
		$path = str_replace($root,'',$path);
		$url = substr(str_replace($path,'',$url),0,-1);
		return 'http://'.preg_replace("/\:\d+/", '', $_SERVER['HTTP_HOST']).($_SERVER['SERVER_PORT'] && $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '').$url;
	}
}

function xCopy($source,$destination,$child)
{
	//用法：
	//   xCopy("feiy","feiy2",1):拷贝feiy下的文件到   feiy2,包括子目录
	//   xCopy("feiy","feiy2",0):拷贝feiy下的文件到   feiy2,不包括子目录
	//参数说明：
	//   $source:源目录名
	//   $destination:目的目录名
	//   $child:复制时，是不是包含的子目录
	if(!is_dir($source))
	{
	  echo("Error:the   $source   is   not   a   direction!");
	  return   0;
	}
	if(!is_dir($destination))
		mkdir($destination,0777);


	$handle=dir($source);
	while($entry=$handle->read())
	{
		if(($entry!=".")&&($entry!=".."))
		{
			if(is_dir($source."/".$entry))
			{
				if($child)
					xCopy($source."/".$entry,$destination."/".$entry,$child);
			}
			else
			{
				copy($source."/".$entry,$destination."/".$entry);
			}
		}
	}

	return   1;
}

function showjsmessage($message,$isBack = 0){
	echo "<script type=\"text/javascript\">showmessage(\"".$message."\",".$isBack.");</script>"."\r\n";
	flush();
	ob_flush();
}

//全站通用的清除所有缓存的方法
function clear_cache()
{
	clearDir(ROOT_PATH."admin/Runtime/");
	clearDir(ROOT_PATH."install/Runtime/Cache/");
}

function clearDir($dir)
{
	if(!file_exists($dir))
		return;

	$directory = dir($dir);

	while($entry = $directory->read())
	{
		if($entry != '.' && $entry != '..')
		{
			$filename = $dir.'/'.$entry;
			if(is_dir($filename))
				clearDir($filename);

			if(is_file($filename))
				@unlink($filename);
		}
	}

	$directory->close();
}

//由数据库取出系统的配置
function fanweC($name)
{
	return C($name);
}

function getDomain($nav_url,$bbs_url)
{
	$nav_host = parse_url($nav_url);
	$nav_host = $nav_host['host'];
	$nav_domain = explode(".", $nav_host);
	$domain_count = count($nav_domain);
	if($domain_count > 2)
		$nav_domain = $nav_domain[$domain_count-2].".".$nav_domain[$domain_count-1];
	else
		$nav_domain = $nav_host;

	$bbs_host = parse_url($bbs_url);
	$bbs_host = $bbs_host['host'];
	$bbs_domain = explode(".", $bbs_host);
	$domain_count = count($bbs_domain);
	if($domain_count > 2)
		$bbs_domain = $bbs_domain[$domain_count-2].".".$bbs_domain[$domain_count-1];
	else
		$bbs_domain = $bbs_host;

	if($nav_domain == $bbs_domain)
		return $nav_domain;
	else
		false;
}

//由数据库取出系统的配置
function fanweDBC($name)
{
	static $sysConf = array();

	if ($sysConf[$name] === NULL)
	{
        $db_config = include ROOT_PATH."public/db_config.php";
        $db = Db::getInstance(array('dbms'=>'mysql','hostname'=>$db_config['DB_HOST'],'username'=>$db_config['DB_USER'],'password'=>$db_config['DB_PWD'],'hostport'=>$db_config['DB_PORT'],'database'=>$db_config['DB_NAME']));

		$value = $db->query('SELECT val FROM '.$db_config['DB_PREFIX']."sys_conf WHERE name = '$name'");
		if(count($value) > 0)
			$sysConf[$name] = $value[0]['val'];
		else
			$sysConf[$name] = false;
	}

	return $sysConf[$name];
}

function toDate ($time, $format = 'Y-m-d H:i:s')
{
	if (empty($time))
	{
		return '';
	}
	$timezone = intval(fanweDBC('TIME_ZONE'));
	$time = $time + $timezone * 3600;
	$format = str_replace('#', ':', $format);
	return date($format, $time);
}

function str2Time($str)
{
	$str = trim($str);
	if(empty($str))
		return 0;

	$timezone = intval(fanweDBC('TIME_ZONE'));

	$time = strtotime($str) - $timezone * 3600;
	return $time;
}

function random($length, $numeric = 0)
{
	$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
	$hash = '';
	$max = strlen($seed) - 1;
	for($i = 0; $i < $length; $i++)
	{
		$hash .= $seed{mt_rand(0, $max)};
	}
	return $hash;
}
?>